package DDG::Goodie::Chess960;
# ABSTRACT: return a random Chess 960 starting position.

use strict;
use DDG::Goodie;

triggers any => 'random', 'chess960';
zci is_cached => 0;
zci answer_type => 'chess960_position';

my @all_positions = qw(
BBQNNRKR BQNBNRKR BQNNRBKR BQNNRKRB QBBNNRKR QNBBNRKR QNBNRBKR QNBNRKRB QBNNBRKR QNNBBRKR
QNNRBBKR QNNRBKRB QBNNRKBR QNNBRKBR QNNRKBBR QNNRKRBB BBNQNRKR BNQBNRKR BNQNRBKR BNQNRKRB
NBBQNRKR NQBBNRKR NQBNRBKR NQBNRKRB NBQNBRKR NQNBBRKR NQNRBBKR NQNRBKRB NBQNRKBR NQNBRKBR
NQNRKBBR NQNRKRBB BBNNQRKR BNNBQRKR BNNQRBKR BNNQRKRB NBBNQRKR NNBBQRKR NNBQRBKR NNBQRKRB
NBNQBRKR NNQBBRKR NNQRBBKR NNQRBKRB NBNQRKBR NNQBRKBR NNQRKBBR NNQRKRBB BBNNRQKR BNNBRQKR
BNNRQBKR BNNRQKRB NBBNRQKR NNBBRQKR NNBRQBKR NNBRQKRB NBNRBQKR NNRBBQKR NNRQBBKR NNRQBKRB
NBNRQKBR NNRBQKBR NNRQKBBR NNRQKRBB BBNNRKQR BNNBRKQR BNNRKBQR BNNRKQRB NBBNRKQR NNBBRKQR
NNBRKBQR NNBRKQRB NBNRBKQR NNRBBKQR NNRKBBQR NNRKBQRB NBNRKQBR NNRBKQBR NNRKQBBR NNRKQRBB
BBNNRKRQ BNNBRKRQ BNNRKBRQ BNNRKRQB NBBNRKRQ NNBBRKRQ NNBRKBRQ NNBRKRQB NBNRBKRQ NNRBBKRQ
NNRKBBRQ NNRKBRQB NBNRKRBQ NNRBKRBQ NNRKRBBQ NNRKRQBB BBQNRNKR BQNBRNKR BQNRNBKR BQNRNKRB
QBBNRNKR QNBBRNKR QNBRNBKR QNBRNKRB QBNRBNKR QNRBBNKR QNRNBBKR QNRNBKRB QBNRNKBR QNRBNKBR
QNRNKBBR QNRN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NQ RKNRBNQB
RBKNRNBQ RKNBRNBQ RKNRNBBQ RKNRNQBB BBQRKRNN BQRBKRNN BQRKRBNN BQRKRNNB QBBRKRNN QRBBKRNN
QRBKRBNN QRBKRNNB QBRKBRNN QRKBBRNN QRKRBBNN QRKRBNNB QBRKRNBN QRKBRNBN QRKRNBBN QRKRNNBB
BBRQKRNN BRQBKRNN BRQKRBNN BRQKRNNB RBBQKRNN RQBBKRNN RQBKRBNN RQBKRNNB RBQKBRNN RQKBBRNN
RQKRBBNN RQKRBNNB RBQKRNBN RQKBRNBN RQKRNBBN RQKRNNBB BBRKQRNN BRKBQRNN BRKQRBNN BRKQRNNB
RBBKQRNN RKBBQRNN RKBQRBNN RKBQRNNB RBKQBRNN RKQBBRNN RKQRBBNN RKQRBNNB RBKQRNBN RKQBRNBN
RKQRNBBN RKQRNNBB BBRKRQNN BRKBRQNN BRKRQBNN BRKRQNNB RBBKRQNN RKBBRQNN RKBRQBNN RKBRQNNB
RBKRBQNN RKRBBQNN RKRQBBNN RKRQBNNB RBKRQNBN RKRBQNBN RKRQNBBN RKRQNNBB BBRKRNQN BRKBRNQN
BRKRNBQN BRKRNQNB RBBKRNQN RKBBRNQN RKBRNBQN RKBRNQNB RBKRBNQN RKRBBNQN RKRNBBQN RKRNBQNB
RBKRNQBN RKRBNQBN RKRNQBBN RKRNQNBB BBRKRNNQ BRKBRNNQ BRKRNBNQ BRKRNNQB RBBKRNNQ RKBBRNNQ
RKBRNBNQ RKBRNNQB RBKRBNNQ RKRBBNNQ RKRNBBNQ RKRNBNQB RBKRNNBQ RKRBNNBQ RKRNNBBQ RKRNNQBB
);

handle query => sub {
    # Ensure rand is seeded for each process
    srand();

    my $query = $_;
    my $pos = undef;
    return unless ($query =~ /\bchess960\b/i &&
            ($query =~ /\brandom\b/i || (($pos) = $query =~ /\b(\d+)\b/))) ;

    my $index = ($pos && 1 <= $pos && $pos <= 960) ? $pos - 1 : int rand @all_positions;

    my $position = $all_positions[$index];
    
    my $position_num = $index + 1;

    $query =~ s/^ chess960|chess960 $|chess960 //i;
    
    return 'Chess 960',
    structured_answer => {
        data => {
            title => 'Chess960',
            subtitle => 'Position ' . $position_num,
            rows => 8,
            columns => 8,
            position => $position
        },
        templates => {
            group => 'text',
            item => 0,
            options => {
                content => 'DDH.chess960.content'
            }
        }
    };
};

1;
